package com.wzg.tools.pdf;

import org.apache.commons.lang3.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @author henryGui
 * @version V1.0
 * @Title: PDFKeywordUtil
 * @Package com.wzg.tools.pdf
 * @Description: TODO
 * @date 2019-08-20 5:57 PM
 */

public class PDFKeywordUtil {

    public static final String MAIN_GSJJ = "公司简介";
    public static final String ONE = "one";
    public static final String TWO = "two";
    public static final String THREE = "three";
    public static final String FOUR = "four";
    public static final String FIVE = "five";
    public static final String SIX = "six";
    public static final String SEVEN = "seven";
    public static final String MAIN_CWKJXX = "财务会计信息";


    public static void main(String[] args) throws MalformedURLException {
        process();
    }


    private static Map getKeyWordList() {
        Map<String, Map<String, String[]>> map = new HashMap<String, Map<String, String[]>>();

        String[] one = {"33333","法定名称及缩写"};
        String[] two = {"1111111","注册资本"};
        String[] three = {"公司住所和营业场所", "注册地"};
        String[] four = {"成立时间"};
        String[] five = {"经营范围和经营区域"};
        String[] six = {"法定代表人"};
        String[] seven = {"客服电话投诉渠道和投诉处理程序","客服电话和投诉电话"};

        Map<String, String[]> gsjj = new HashMap<String, String[]>();
        gsjj.put(ONE, one);
        gsjj.put(TWO, two);
        gsjj.put(THREE, three);
        gsjj.put(FOUR, four);
        gsjj.put(FIVE, five);
        gsjj.put(SIX, six);
        gsjj.put(SEVEN, seven);
        map.put(MAIN_GSJJ, gsjj);
        return map;
    }

    public static void process() throws MalformedURLException {
        String content = PDFUtil.readPdfURL(
                new URL("http://icid.iachina.cn/files/piluxinxi/pdf/22f4b52f-b6ef-4de8-acbd-31d610cfffc4.PDF"),
//                new URL("http://icid.iachina.cn/files/piluxinxi/pdf/e063e9dd-7530-4163-ae2b-848f6353917f.PDF"),
                1, 3);
        System.out.println(content);

        Map<String, Map<String, String[]>> keywordMap = getKeyWordList();

//        for(Map.Entry<String, Map<String, String[]>> entry: keywordMap.entrySet()){
//            System.out.println(entry.getKey()+"="+entry.getValue());
//
//        }
        processGsjj(content,keywordMap);

    }

    private static void processGsjj(String content, Map<String, Map<String, String[]>> keywordMap) {

        String[] ones = keywordMap.get(MAIN_GSJJ).get(ONE);

        String[] twos = keywordMap.get(MAIN_GSJJ).get(TWO);
        String[] threes = keywordMap.get(MAIN_GSJJ).get(THREE);
        String[] fours = keywordMap.get(MAIN_GSJJ).get(FOUR);
        String[] fives = keywordMap.get(MAIN_GSJJ).get(FIVE);
        String[] sixs = keywordMap.get(MAIN_GSJJ).get(SIX);
        String[] sevens = keywordMap.get(MAIN_GSJJ).get(SEVEN);

        System.out.println("==========================");
//        String[] ones_=indexOfKeyword(content,ones,twos);
//        System.out.println(ones_[0]+"======="+ones_[1].replace("（二）",""));
//        String[] twos_=indexOfKeyword(content,twos,threes);
//        System.out.println(twos_[0]+"======="+twos_[1].replace("（三）",""));
//        String[] threes_=indexOfKeyword(content,threes,fours);
//        System.out.println(threes_[0]+"======="+threes_[1].replace("（四）",""));

        println(indexOfKeyword(content,ones,twos));
        println(indexOfKeyword(content,twos,threes));
        println(indexOfKeyword(content,threes,fours));
        println(indexOfKeyword(content,fours,fives));
        println(indexOfKeyword(content,fives,sixs));
        println(indexOfKeyword(content,sixs,sevens));
        println(indexOfKeyword(content,sevens,new String[]{MAIN_CWKJXX}));

//        System.out.println("95542 \n" + " \n" + " \n" + " \n" + " \n" + " 2 / 38 ".replaceAll("\\d+ / \\d+",""));




//        for (Map.Entry<String, String[]> entry : keywordMap.get("公司简介").entrySet()) {
//            System.out.println(entry.getKey() + "=" + entry.getValue());
//
//        }
    }

    private static void println(String[] data){
        String regex="[（[一|二|三|四|五|六|七]）|二、]";
        System.out.println(data[0]+"======="+data[1].replaceAll(regex,"").replaceAll("\\d+ / \\d+",""));
    }

    private static String[] indexOfKeyword(String content,String[] sources,String indexKeys[]){
        String[] keywordContent=new String[2];
        for (String source:sources) {
            if(indexKeys==null|| indexKeys.length==0){
                keywordContent[1]=content.substring(content.indexOf(source)+source.length());
                keywordContent[0]=source;
                break;
            }else{
                for(String indexKey:indexKeys){
                    if(StringUtils.isNotBlank(content) && content.indexOf(source)>=0 && content.indexOf(indexKey)>=0){
                        keywordContent[1]=content.substring(content.indexOf(source)+source.length(),content.indexOf(indexKey));
                        keywordContent[0]=source;
                    }
                    if(StringUtils.isNotBlank(keywordContent[1])){
                        break;
                    }
                }
            }
        }
        return keywordContent;
    }


}
